
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>JS数组reduce()方法详解及高级技巧 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="js中获取时间new Date详细介绍.html" />
    
    
    <link rel="prev" href="js时间戳与日期格式之间相互转换.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../cmd命令.html">
            
                <a href="../cmd命令.html">
            
                    
                    cmd命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="Array.prototype.slice.call.html">
            
                <a href="Array.prototype.slice.call.html">
            
                    
                    Array.prototype.slice.call解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="async 函数.html">
            
                <a href="async 函数.html">
            
                    
                    async 函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="callback&&callback如果存在回调函数就执行.html">
            
                <a href="callback&&callback如果存在回调函数就执行.html">
            
                    
                    callback&&callback() 如果存在回调函数就执行！
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="CSS3 nth-child使用注意.html">
            
                <a href="CSS3 nth-child使用注意.html">
            
                    
                    CSS3 nth-child(n)使用注意
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="css3的 -webkit-min-device-pixel-ratio属性 解决高清的适配.html">
            
                <a href="css3的 -webkit-min-device-pixel-ratio属性 解决高清的适配.html">
            
                    
                    css3的 -webkit-min-device-pixel-ratio属性 解决高清的适配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="CSS文本超出自动缩小字体.html">
            
                <a href="CSS文本超出自动缩小字体.html">
            
                    
                    CSS文本超出自动缩小字体
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="element-ui默认样式修改.html">
            
                <a href="element-ui默认样式修改.html">
            
                    
                    element-ui默认样式修改
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="JavaScript setInterval函数详解.html">
            
                <a href="JavaScript setInterval函数详解.html">
            
                    
                    JavaScript setInterval() 函数详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="JavaScript概念之screenclientoffsetscrollinneravail的widthleft.html">
            
                <a href="JavaScript概念之screenclientoffsetscrollinneravail的widthleft.html">
            
                    
                    JavaScript概念之screenclientoffsetscrollinneravail的widthleft
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="js获取星期几.html">
            
                <a href="js获取星期几.html">
            
                    
                    js获取星期几
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="js监听滚动条事件和获取浏览器及屏幕高度和宽度等信息的方法.html">
            
                <a href="js监听滚动条事件和获取浏览器及屏幕高度和宽度等信息的方法.html">
            
                    
                    js监听滚动条事件和获取浏览器及屏幕高度和宽度等信息的方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="js时间戳与日期格式之间相互转换.html">
            
                <a href="js时间戳与日期格式之间相互转换.html">
            
                    
                    js时间戳与日期格式之间相互转换
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.15" data-path="JS数组reduce方法详解及高级技巧.html">
            
                <a href="JS数组reduce方法详解及高级技巧.html">
            
                    
                    JS数组reduce()方法详解及高级技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="js中获取时间new Date详细介绍.html">
            
                <a href="js中获取时间new Date详细介绍.html">
            
                    
                    js中获取时间new Date()详细介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="less传参问题：在url中插入变量今天在需要用less给url传递参数时遇到了小问题，一番搜索后有了下述解决方法.html">
            
                <a href="less传参问题：在url中插入变量今天在需要用less给url传递参数时遇到了小问题，一番搜索后有了下述解决方法.html">
            
                    
                    less传参问题：在url中插入变量今天在需要用less给url传递参数时遇到了小问题，一番搜索后有了下述解决方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="moment 和date-fns.html">
            
                <a href="moment 和date-fns.html">
            
                    
                    moment 和date-fns
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="node.JS中配置http-server.html">
            
                <a href="node.JS中配置http-server.html">
            
                    
                    node.JS中配置http-server
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="Object.keys详解.html">
            
                <a href="Object.keys详解.html">
            
                    
                    Object.keys详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="router-link各种属性解释.html">
            
                <a href="router-link各种属性解释.html">
            
                    
                    router-link各种属性解释
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="stylus使用介绍.html">
            
                <a href="stylus使用介绍.html">
            
                    
                    stylus使用介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23" data-path="TypeError Cannot read property 'compilation' of undefined.html">
            
                <a href="TypeError Cannot read property 'compilation' of undefined.html">
            
                    
                    TypeError Cannot read property 'compilation' of undefined
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.24" data-path="Vue  keep-alive.html">
            
                <a href="Vue  keep-alive.html">
            
                    
                    Vue  keep-alive
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.25" data-path="Vue - 过渡与动画transition.html">
            
                <a href="Vue - 过渡与动画transition.html">
            
                    
                    Vue - 过渡与动画transition
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.26" data-path="vue-axios使用方法.html">
            
                <a href="vue-axios使用方法.html">
            
                    
                    vue-axios使用方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.27" data-path="vue-calendar-component.html">
            
                <a href="vue-calendar-component.html">
            
                    
                    vue-calendar-component
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.28" data-path="vue-infinite-loading.html">
            
                <a href="vue-infinite-loading.html">
            
                    
                    vue-infinite-loading
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.29" data-path="vue报错There are multiple modules with names that only differ in casing.html">
            
                <a href="vue报错There are multiple modules with names that only differ in casing.html">
            
                    
                    vue报错There are multiple modules with names that only differ in casing
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.30" data-path="Vue中注意target和currentTarget的使用.html">
            
                <a href="Vue中注意target和currentTarget的使用.html">
            
                    
                    Vue中注意target和currentTarget的使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.31" data-path="关于给javascript对象添加、删除、修改对象的属性.html">
            
                <a href="关于给javascript对象添加、删除、修改对象的属性.html">
            
                    
                    关于给javascript对象添加、删除、修改对象的属性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.32" data-path="获取当前地址坐标.html">
            
                <a href="获取当前地址坐标.html">
            
                    
                    获取当前地址坐标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.33" data-path="解决ajax跨域问题【5种解决方案】.html">
            
                <a href="解决ajax跨域问题【5种解决方案】.html">
            
                    
                    解决ajax跨域问题【5种解决方案】
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.34" data-path="解决vue-cli webpack打包后加载资源的路径问题.html">
            
                <a href="解决vue-cli webpack打包后加载资源的路径问题.html">
            
                    
                    解决vue-cli webpack打包后加载资源的路径问题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.35" data-path="时间格式转换：201971转换为2019-7-1.html">
            
                <a href="时间格式转换：201971转换为2019-7-1.html">
            
                    
                    时间格式转换：201971转换为2019-7-1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.36" data-path="数据库密码改了.html">
            
                <a href="数据库密码改了.html">
            
                    
                    数据库密码改了
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.37" data-path="语法getBoundingClientRect.html">
            
                <a href="语法getBoundingClientRect.html">
            
                    
                    语法getBoundingClientRect()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.38" data-path="在js中使用length报错Error in renderTypeErrorcannot read property bgImg of null.html">
            
                <a href="在js中使用length报错Error in renderTypeErrorcannot read property bgImg of null.html">
            
                    
                    在js中使用length报错Error in renderTypeErrorcannot read property bgImg of null
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >JS数组reduce()方法详解及高级技巧</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="js&#x6570;&#x7EC4;reduce&#x65B9;&#x6CD5;&#x8BE6;&#x89E3;&#x53CA;&#x9AD8;&#x7EA7;&#x6280;&#x5DE7;">JS&#x6570;&#x7EC4;reduce()&#x65B9;&#x6CD5;&#x8BE6;&#x89E3;&#x53CA;&#x9AD8;&#x7EA7;&#x6280;&#x5DE7;</h1>
<blockquote>
<p>reduce()&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x641E;&#x5B9A;&#x7684;&#x4E1C;&#x897F;&#xFF0C;for&#x5FAA;&#x73AF;&#xFF0C;&#x6216;&#x8005;forEach&#x65B9;&#x6CD5;&#x6709;&#x65F6;&#x5019;&#x4E5F;&#x53EF;&#x4EE5;&#x641E;&#x5B9A;&#xFF0C;&#x90A3;&#x4E3A;&#x5565;&#x8981;&#x7528;reduce()&#xFF1F;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#xFF0C;&#x4E4B;&#x524D;&#x6211;&#x4E5F;&#x60F3;&#x8FC7;&#xFF0C;&#x8981;&#x8BF4;&#x539F;&#x56E0;&#x8FD8;&#x771F;&#x627E;&#x4E0D;&#x5230;&#xFF0C;&#x552F;&#x4E00;&#x80FD;&#x627E;&#x5230;&#x7684;&#x662F;&#xFF1A;&#x901A;&#x5F80;&#x6210;&#x529F;&#x7684;&#x9053;&#x8DEF;&#x6709;&#x5F88;&#x591A;&#xFF0C;&#x4F46;&#x662F;&#x603B;&#x6709;&#x4E00;&#x6761;&#x8DEF;&#x662F;&#x6700;&#x6377;&#x5F84;&#x7684;&#xFF0C;&#x4EA6;&#x6216;&#x8BB8;reduce()&#x903C;&#x683C;&#x66F4;&#x9AD8;...</p>
</blockquote>
<h4 id="1&#x3001;&#x8BED;&#x6CD5;">1&#x3001;&#x8BED;&#x6CD5;</h4>
<pre><code>arr.reduce(callback,[initialValue])
</code></pre><p>reduce &#x4E3A;&#x6570;&#x7EC4;&#x4E2D;&#x7684;&#x6BCF;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x4F9D;&#x6B21;&#x6267;&#x884C;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF0C;&#x4E0D;&#x5305;&#x62EC;&#x6570;&#x7EC4;&#x4E2D;&#x88AB;&#x5220;&#x9664;&#x6216;&#x4ECE;&#x672A;&#x88AB;&#x8D4B;&#x503C;&#x7684;&#x5143;&#x7D20;&#xFF0C;&#x63A5;&#x53D7;&#x56DB;&#x4E2A;&#x53C2;&#x6570;&#xFF1A;&#x521D;&#x59CB;&#x503C;&#xFF08;&#x6216;&#x8005;&#x4E0A;&#x4E00;&#x6B21;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#xFF09;&#xFF0C;&#x5F53;&#x524D;&#x5143;&#x7D20;&#x503C;&#xFF0C;&#x5F53;&#x524D;&#x7D22;&#x5F15;&#xFF0C;&#x8C03;&#x7528; reduce &#x7684;&#x6570;&#x7EC4;&#x3002;</p>
<pre><code>callback &#xFF08;&#x6267;&#x884C;&#x6570;&#x7EC4;&#x4E2D;&#x6BCF;&#x4E2A;&#x503C;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x5305;&#x542B;&#x56DB;&#x4E2A;&#x53C2;&#x6570;&#xFF09;

    1&#x3001;previousValue &#xFF08;&#x4E0A;&#x4E00;&#x6B21;&#x8C03;&#x7528;&#x56DE;&#x8C03;&#x8FD4;&#x56DE;&#x7684;&#x503C;&#xFF0C;&#x6216;&#x8005;&#x662F;&#x63D0;&#x4F9B;&#x7684;&#x521D;&#x59CB;&#x503C;&#xFF08;initialValue&#xFF09;&#xFF09;
    2&#x3001;currentValue &#xFF08;&#x6570;&#x7EC4;&#x4E2D;&#x5F53;&#x524D;&#x88AB;&#x5904;&#x7406;&#x7684;&#x5143;&#x7D20;&#xFF09;
    3&#x3001;index &#xFF08;&#x5F53;&#x524D;&#x5143;&#x7D20;&#x5728;&#x6570;&#x7EC4;&#x4E2D;&#x7684;&#x7D22;&#x5F15;&#xFF09;
    4&#x3001;array &#xFF08;&#x8C03;&#x7528; reduce &#x7684;&#x6570;&#x7EC4;&#xFF09;

initialValue &#xFF08;&#x4F5C;&#x4E3A;&#x7B2C;&#x4E00;&#x6B21;&#x8C03;&#x7528; callback &#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x3002;&#xFF09;
</code></pre><h4 id="2&#x3001;&#x5B9E;&#x4F8B;&#x89E3;&#x6790;-initialvalue-&#x53C2;&#x6570;">2&#x3001;&#x5B9E;&#x4F8B;&#x89E3;&#x6790; initialValue &#x53C2;&#x6570;</h4>
<p>&#x5148;&#x770B;&#x7B2C;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code>var arr = [1, 2, 3, 4];
var sum = arr.reduce(function(prev, cur, index, arr) {
    console.log(prev, cur, index);
    return prev + cur;
})
console.log(arr, sum);
</code></pre><blockquote>
<p>&#x6253;&#x5370;&#x7ED3;&#x679C;&#xFF1A;
 1 2 1
 3 3 2
 6 4 3
 [1, 2, 3, 4] 10</p>
</blockquote>
<p>&#x8FD9;&#x91CC;&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#xFF0C;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;index&#x662F;&#x4ECE;1&#x5F00;&#x59CB;&#x7684;&#xFF0C;&#x7B2C;&#x4E00;&#x6B21;&#x7684;prev&#x7684;&#x503C;&#x662F;&#x6570;&#x7EC4;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x503C;&#x3002;&#x6570;&#x7EC4;&#x957F;&#x5EA6;&#x662F;4&#xFF0C;&#x4F46;&#x662F;reduce&#x51FD;&#x6570;&#x5FAA;&#x73AF;3&#x6B21;&#x3002;</p>
<p>&#x518D;&#x770B;&#x7B2C;&#x4E8C;&#x4E2A;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code>var  arr = [1, 2, 3, 4];
var sum = arr.reduce(function(prev, cur, index, arr) {
    console.log(prev, cur, index);
    return prev + cur;
}&#xFF0C;0) //&#x6CE8;&#x610F;&#x8FD9;&#x91CC;&#x8BBE;&#x7F6E;&#x4E86;&#x521D;&#x59CB;&#x503C;
console.log(arr, sum);
</code></pre><blockquote>
<p>&#x6253;&#x5370;&#x7ED3;&#x679C;&#xFF1A;
 0 1 0
 1 2 1
 3 3 2
 6 4 3
 [1, 2, 3, 4] 10</p>
</blockquote>
<p>&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;index&#x662F;&#x4ECE;0&#x5F00;&#x59CB;&#x7684;&#xFF0C;&#x7B2C;&#x4E00;&#x6B21;&#x7684;prev&#x7684;&#x503C;&#x662F;&#x6211;&#x4EEC;&#x8BBE;&#x7F6E;&#x7684;&#x521D;&#x59CB;&#x503C;0&#xFF0C;&#x6570;&#x7EC4;&#x957F;&#x5EA6;&#x662F;4&#xFF0C;reduce&#x51FD;&#x6570;&#x5FAA;&#x73AF;4&#x6B21;&#x3002;</p>
<p>&#x7ED3;&#x8BBA;&#xFF1A;<code>&#x5982;&#x679C;&#x6CA1;&#x6709;&#x63D0;&#x4F9B;initialValue&#xFF0C;reduce &#x4F1A;&#x4ECE;&#x7D22;&#x5F15;1&#x7684;&#x5730;&#x65B9;&#x5F00;&#x59CB;&#x6267;&#x884C; callback &#x65B9;&#x6CD5;&#xFF0C;&#x8DF3;&#x8FC7;&#x7B2C;&#x4E00;&#x4E2A;&#x7D22;&#x5F15;&#x3002;&#x5982;&#x679C;&#x63D0;&#x4F9B;initialValue&#xFF0C;&#x4ECE;&#x7D22;&#x5F15;0&#x5F00;&#x59CB;&#x3002;</code></p>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x5982;&#x679C;&#x8FD9;&#x4E2A;&#x6570;&#x7EC4;&#x4E3A;&#x7A7A;&#xFF0C;&#x8FD0;&#x7528;reduce&#x662F;&#x4EC0;&#x4E48;&#x60C5;&#x51B5;&#xFF1F;</p>
<pre><code>var  arr = [];
var sum = arr.reduce(function(prev, cur, index, arr) {
    console.log(prev, cur, index);
    return prev + cur;
})
//&#x62A5;&#x9519;&#xFF0C;&quot;TypeError: Reduce of empty array with no initial value&quot;
</code></pre><p>&#x4F46;&#x662F;&#x8981;&#x662F;&#x6211;&#x4EEC;&#x8BBE;&#x7F6E;&#x4E86;&#x521D;&#x59CB;&#x503C;&#x5C31;&#x4E0D;&#x4F1A;&#x62A5;&#x9519;&#xFF0C;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>var  arr = [];
var sum = arr.reduce(function(prev, cur, index, arr) {
    console.log(prev, cur, index);
    return prev + cur;
}&#xFF0C;0)
console.log(arr, sum); // [] 0
</code></pre><p><strong>&#x6240;&#x4EE5;&#x4E00;&#x822C;&#x6765;&#x8BF4;&#x6211;&#x4EEC;&#x63D0;&#x4F9B;&#x521D;&#x59CB;&#x503C;&#x901A;&#x5E38;&#x66F4;&#x5B89;&#x5168;</strong></p>
<h4 id="3&#x3001;reduce&#x7684;&#x7B80;&#x5355;&#x7528;&#x6CD5;">3&#x3001;reduce&#x7684;&#x7B80;&#x5355;&#x7528;&#x6CD5;</h4>
<p>&#x5F53;&#x7136;&#x6700;&#x7B80;&#x5355;&#x7684;&#x5C31;&#x662F;&#x6211;&#x4EEC;&#x5E38;&#x7528;&#x7684;&#x6570;&#x7EC4;&#x6C42;&#x548C;&#xFF0C;&#x6C42;&#x4E58;&#x79EF;&#x4E86;&#x3002;</p>
<pre><code>var  arr = [1, 2, 3, 4];
var sum = arr.reduce((x,y)=&gt;x+y)
var mul = arr.reduce((x,y)=&gt;x*y)
console.log( sum ); //&#x6C42;&#x548C;&#xFF0C;10
console.log( mul ); //&#x6C42;&#x4E58;&#x79EF;&#xFF0C;24
</code></pre><h4 id="4&#x3001;reduce&#x7684;&#x9AD8;&#x7EA7;&#x7528;&#x6CD5;">4&#x3001;reduce&#x7684;&#x9AD8;&#x7EA7;&#x7528;&#x6CD5;</h4>
<p>&#xFF08;1&#xFF09;&#x8BA1;&#x7B97;&#x6570;&#x7EC4;&#x4E2D;&#x6BCF;&#x4E2A;&#x5143;&#x7D20;&#x51FA;&#x73B0;&#x7684;&#x6B21;&#x6570;</p>
<pre><code>let names = [&apos;Alice&apos;, &apos;Bob&apos;, &apos;Tiff&apos;, &apos;Bruce&apos;, &apos;Alice&apos;];

let nameNum = names.reduce((pre,cur)=&gt;{
  if(cur in pre){
    pre[cur]++
  }else{
    pre[cur] = 1 
  }
  return pre
},{})
console.log(nameNum); //{Alice: 2, Bob: 1, Tiff: 1, Bruce: 1}
</code></pre><p>&#xFF08;2&#xFF09;&#x6570;&#x7EC4;&#x53BB;&#x91CD;</p>
<pre><code>let arr = [1,2,3,4,4,1]
let newArr = arr.reduce((pre,cur)=&gt;{
    if(!pre.includes(cur)){
      return pre.concat(cur)
    }else{
      return pre
    }
},[])
console.log(newArr);// [1, 2, 3, 4]
</code></pre><p>&#xFF08;3&#xFF09;&#x5C06;&#x4E8C;&#x7EF4;&#x6570;&#x7EC4;&#x8F6C;&#x5316;&#x4E3A;&#x4E00;&#x7EF4;</p>
<pre><code>let arr = [[0, 1], [2, 3], [4, 5]]
let newArr = arr.reduce((pre,cur)=&gt;{
    return pre.concat(cur)
},[])
console.log(newArr); // [0, 1, 2, 3, 4, 5]
</code></pre><p>&#xFF08;3&#xFF09;&#x5C06;&#x591A;&#x7EF4;&#x6570;&#x7EC4;&#x8F6C;&#x5316;&#x4E3A;&#x4E00;&#x7EF4;</p>
<pre><code>let arr = [[0, 1], [2, 3], [4,[5,6,7]]]
const newArr = function(arr){
   return arr.reduce((pre,cur)=&gt;pre.concat(Array.isArray(cur)?newArr(cur):cur),[])
}
console.log(newArr(arr)); //[0, 1, 2, 3, 4, 5, 6, 7]
</code></pre><p>&#xFF08;4&#xFF09;&#x3001;&#x5BF9;&#x8C61;&#x91CC;&#x7684;&#x5C5E;&#x6027;&#x6C42;&#x548C;</p>
<pre><code>var result = [
    {
        subject: &apos;math&apos;,
        score: 10
    },
    {
        subject: &apos;chinese&apos;,
        score: 20
    },
    {
        subject: &apos;english&apos;,
        score: 30
    }
];

var sum = result.reduce(function(prev, cur) {
    return cur.score + prev;
}, 0);
console.log(sum) //60
</code></pre><p>&#x4F5C;&#x8005;&#xFF1A;<em>littleTank</em></p>
<p>&#x94FE;&#x63A5;&#xFF1A;<a href="https://www.jianshu.com/p/e375ba1cfc47" target="_blank">https://www.jianshu.com/p/e375ba1cfc47</a></p>
<p>&#x6765;&#x6E90;&#xFF1A;&#x7B80;&#x4E66;</p>
<p>&#x7B80;&#x4E66;&#x8457;&#x4F5C;&#x6743;&#x5F52;&#x4F5C;&#x8005;&#x6240;&#x6709;&#xFF0C;&#x4EFB;&#x4F55;&#x5F62;&#x5F0F;&#x7684;&#x8F6C;&#x8F7D;&#x90FD;&#x8BF7;&#x8054;&#x7CFB;&#x4F5C;&#x8005;&#x83B7;&#x5F97;&#x6388;&#x6743;&#x5E76;&#x6CE8;&#x660E;&#x51FA;&#x5904;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="js时间戳与日期格式之间相互转换.html" class="navigation navigation-prev " aria-label="Previous page: js时间戳与日期格式之间相互转换">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="js中获取时间new Date详细介绍.html" class="navigation navigation-next " aria-label="Next page: js中获取时间new Date()详细介绍">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"JS数组reduce()方法详解及高级技巧","level":"1.15","depth":1,"next":{"title":"js中获取时间new Date()详细介绍","level":"1.16","depth":1,"path":"project/js中获取时间new Date详细介绍.md","ref":"project/js中获取时间new Date详细介绍.md","articles":[]},"previous":{"title":"js时间戳与日期格式之间相互转换","level":"1.14","depth":1,"path":"project/js时间戳与日期格式之间相互转换.md","ref":"project/js时间戳与日期格式之间相互转换.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"project/JS数组reduce方法详解及高级技巧.md","mtime":"2019-06-16T15:39:16.984Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-07-18T03:07:23.077Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

